# -*- coding:utf-8 -*-
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton,
                             QHBoxLayout, QVBoxLayout, QApplication,
                             QLineEdit,QTextEdit,QListWidget,QListWidgetItem,
                             QTableWidget,QTableWidgetItem,QSplitter,
                             QCheckBox)
from PyQt5.QtWebEngineWidgets import QWebEnginePage,QWebEngineView
from PyQt5.QtCore import QTimer,Qt,QUrl
import time
import csv
from multiprocessing import Process
import HDYclass
import tushare as ts
import pandas
from collections import Counter
import jieba.analyse
import jieba

filename=0
test=0
t=0
text=0

answer_revised=[]

#深交所互动易精华回答内容抓取
Elite_HDYcontent = HDYclass.Elite_HDY()
Elite_HDYcontent.answercontent(answer_revised)

#统计数据
sum=0

#上榜家数统计
ComNum=len(HDYclass.stockbox)

stock_statistic=[]

for stock in HDYclass.stockbox:
    # 计算5日涨幅之和最新交易日涨幅
    per = ((ts.get_hist_data(stock[-7:-1]).iloc[:, 0:4].iloc[0, 2] / ts.get_hist_data(stock[-7:-1]).iloc[:, 0:4].iloc[5, 2]) - 1) * 100
    perT = ((ts.get_hist_data(stock[-7:-1]).iloc[:, 0:4].iloc[0, 2] / ts.get_hist_data(stock[-7:-1]).iloc[:, 0:4].iloc[1, 2]) - 1) * 100
    sum += per
    if stock not in stock_statistic:
        stock_statistic.append([stock, round(perT, 2), round(per, 2)])

sum=round(sum/len(HDYclass.stockbox),2)

#计算上证指数5日涨幅
SHindex5day=round(((ts.get_hist_data('sh').iloc[:, 0:4].iloc[0, 2] / ts.get_hist_data('sh').iloc[:, 0:4].iloc[5, 2]) - 1) * 100,2)

if sum>SHindex5day and SHindex5day>0:
    check_str='对散户问题认真回复的都是好公司！\n买买买！'
elif sum>SHindex5day and sum<0:
    check_str = '能跑赢大盘挺好了,要求别太高'
else:
    check_str ='连大盘也跑不过！退市算了！'

statistics_str='最新回复中有'+str(ComNum)+'条精华回复\n所涉及的个股五日平均涨幅为'+str(sum)+'%\n上证指数5日涨幅为'+str(SHindex5day)+'%\n' +check_str


# 界面设计
class Example(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):

        # 设定布局

        # 互动易展示区域
        self.hudongyi = QTableWidget(len(HDYclass.Elite_all_answer), 4)
        self.hudongyi.verticalHeader().setVisible(False)
        self.hudongyi.setHorizontalHeaderLabels(['个股名称', '提问内容', '回复内容', '回复时间'])

        # 设置提问与回复框的单元格宽度及整体高度
        self.hudongyi.setColumnWidth(1, 400)
        self.hudongyi.setColumnWidth(2, 400)


        for h in range(100):
            self.hudongyi.setRowHeight(h, 50)

        if len(HDYclass.Elite_all_answer) > 0:

            for i in range(len(HDYclass.Elite_all_answer)):
                for j in range(4):
                    cnt = HDYclass.Elite_all_answer[i][j]

                    newItem = QTableWidgetItem(cnt)
                    self.hudongyi.setItem(i, j, newItem)


        # 数据统计
        data_statistics= QTextEdit()
        data_statistics.setText(statistics_str)

        # 有互动精华回复的个股涨幅统计表格
        self.frequency=QTableWidget(20, 3)
        self.frequency.verticalHeader().setVisible(False)
        self.frequency.setHorizontalHeaderLabels(['个股名称', '单日涨幅','5日涨幅'])


        for a in range(len(stock_statistic)):
            for b in range(3):
                freq_cnt = str(stock_statistic[a][b])

                freq_newItem = QTableWidgetItem(freq_cnt)
                self.frequency.setItem(a, b, freq_newItem)


        # 添加splitter

        # 垂直布局
        Vsplitter=QSplitter(Qt.Vertical)
        Vsplitter.addWidget(data_statistics)
        Vsplitter.addWidget(self.frequency)
        Vsplitter.setStretchFactor(0, 3)
        Vsplitter.setStretchFactor(1, 12)

        #互动易区域与右侧组合框的水平布局
        Hsplitter = QSplitter(Qt.Horizontal)
        Hsplitter.addWidget(self.hudongyi)
        Hsplitter.addWidget(Vsplitter)
        Hsplitter.setStretchFactor(0, 9)
        Hsplitter.setStretchFactor(1, 3)

        MainLayout = QVBoxLayout()
        MainLayout.addWidget(Hsplitter)

        self.setLayout(MainLayout)

        self.setGeometry(50, 50, 1366, 768)
        self.setWindowTitle('互动易')
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
